#!/usr/bin/env python
# $Id: test2-55.py da23e41c6f10 2013/06/16 08:41:30 pgurumur $
# Copyright (c) 2013 Prabhu Gurumurthy <pgurumur@gmail.com>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
# File test2-55.py generated on Wednesday April 03, 2013 @23:53:20 PDT
#
__version__  = "$Revision: da23e41c6f10 $"
__author__   = "$Author: pgurumur $"
__modified__ = "$Date: 2013-06-16 08:41:30Z $"

import hashlib
import base64
import sys

def hmac_sha(text, key):
   k_pad = None
   tk = None

   if len(key) > 64:
      temp = hashlib.sha()
      temp.update(key)
      tk = temp.digest()

   if tk is None:
      k_pad = base64.encodestring(key)
   else:
      k_pad = tk

   foo = ""
   for ix in xrange(64):
      foo += (ord(k_pad[ix]) ^ int(0x36))

   temp = hashlib.sha()
   temp.update(foo)
   temp.update(text)
   digest = temp.digest()

   k_pad = tk
   for ix in xrange(64):
      k_pad[ix] ^= 0x5c

   temp = hashlib.sha()
   temp.update(k_pad)
   temp.update(digest)
   digest = temp.digest()

   print(temp.hexdigest())

def pbkdf2(password, salt, rounds):
   if rounds > 1:
      temp = salt

      flag = True
      count = 1
      while flag:
         temp[len(salt) + 0] = (count >> 24) & 0xFF
         temp[len(salt) + 1] = (count >> 16) & 0xFF
         temp[len(salt) + 2] = (count >> 8) & 0xFF
         temp[len(salt) + 3] = count & 0xFF

if __name__ == "__main__":
   password = sys.argv[1]
   salt = sys.argv[2]
   rounds = 4096

   # pbkdf2(password, salt, rounds)
   hmac_sha(password, salt)
